<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <link rel="stylesheet" href="../static/layui/css/layui.css">
    <script src="../static/layui/layui.js"></script>
    <style>
        .layui-form-selectup dl {
            top: 42px;
            bottom: unset;
        }
    </style>
</head>
<body>

<form class="layui-form" id="deptform" action="" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">部门名称</label>
        <div class="layui-input-block">
            <input type="text" name="name"  placeholder="请输入部门名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门SN</label>
        <div class="layui-input-block">
            <input type="text" name="sn"  placeholder="请输入部门SN" autocomplete="off" class="layui-input">
        </div>
    </div>
</form>


<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
    </div>
</script>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['table','jquery','layer'], function() {
        var table = layui.table;
        var $=layui.jquery;
        var layer=layui.layer;
        table.render({
            elem: '#test'
            , url: '/department/list'
            , toolbar:'#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar:[]
            , title: '部门数据表'
            , cols: [
                [
                {type: 'numbers', title: '编号',align: 'center'}
                ,{field: 'name', title: '部门名', edit: 'text'}
                ,{field: 'sn', title: '部门SN', edit: 'text'}
                ,{field: 'right' ,title: '操作', toolbar:'#barDemo'}
            ]
            ]
            ,parseData:function (res) {
                return{
                    "code":0,
                    "msg":res.success,
                    "count":res.data.length,
                    "data":res.data
                }
            }
        });

        table.on('tool(test)',function (obj) {
            var tr=obj.tr;
            var data=obj.data;
            var layevent=obj.event;
            var deptid=data.id;

            console.log(data);
            if(layevent=="del"){
                $.ajax({
                    url:'/department/delete',
                    type:'post',
                    data:{"id":deptid},
                    dataType:'json',
                    success:function (res) {
                        layer.msg(res.msg);
                        if(res.success){
                            $(tr).remove();
                        }
                    }
                })
            }

            if(layevent=="edit"){


                layer.open({
                    title:"编辑部门",
                    type:1,
                    content:$("#deptform"),
                    area:['500px','300px'],
                    btn:['保存','重置'],

                    btn1:function () {
                        $.ajax({
                            url:'/department/upOrin',
                            type:'post',
                            data:{
                                "id":data.id,
                                "name":$("#deptform").find("input[name='name']").val(),
                                "sn":$("#deptform").find("input[name='sn']").val(),
                            },
                            dataType: 'json',
                            success:function (res) {
                                layer.closeAll('page')
                                $("#deptform").find("input[name='name']").val('');
                                $("#deptform").find("input[name='sn']").val('');
                                layer.msg(res.msg);
                                table.reload("test",{});

                            }

                        })
                    },
                    btn2:function () {
                        $("#deptform").find("input[name='name']").val(data.name);
                        $("#deptform").find("input[name='sn']").val(data.sn);
                        return false;
                    },
                    success:function () {
                        $("#deptform").find("input[name='name']").val(data.name);
                        $("#deptform").find("input[name='sn']").val(data.sn);
                    },

                    cancel:function () {
                        $("#deptform").find("input[name='name']").val('');
                        $("#deptform").find("input[name='sn']").val('');
                    }
                })
            }
        });
        
        table.on('toolbar(test)',function (obj) {
            var layevent=obj.event;
            if (layevent=='add'){

                layer.open({
                    title:"添加部门",
                    type:1,
                    content:$("#deptform"),
                    area:['500px','300px'],
                    btn:['保存','重置'],

                    btn1:function () {
                        $.ajax({
                            url:'/department/upOrin',
                            type:'post',
                            data:{
                                "name":$("#deptform").find("input[name='name']").val(),
                                "sn":$("#deptform").find("input[name='sn']").val(),
                            },
                            dataType: 'json',
                            success:function (res) {
                                layer.closeAll('page')
                                $("#deptform").find("input[name='name']").val('');
                                $("#deptform").find("input[name='sn']").val('');
                                layer.msg(res.msg);
                                table.reload("test",{});

                            }

                        })
                    },
                    btn2:function () {
                        $("#deptform").find("input[name='name']").val('');
                        $("#deptform").find("input[name='sn']").val('');
                        return false;
                    },
                    success:function () {
                        $("#deptform").find("input[name='name']").val('');
                        $("#deptform").find("input[name='sn']").val('');
                    },

                    cancel:function () {
                        $("#deptform").find("input[name='name']").val('');
                        $("#deptform").find("input[name='sn']").val('');
                    }
                })
            }
        })
    })

</script>

</body>
</html>